



           

using System;
using System.Collections.Generic;
using System.Web;
using System.Data.Common;
using Shohdi.DataHandler;
using System.Data;




namespace DatabaseComponent
{
    
    public partial class FoldersDB
    {
          //Write any additional methods or properties here 
          //Don't modify any genrated classes




        /// <summary>
        /// this function will delete the Folders and its subFolders
        /// </summary>
        /// <param name="con">a classdatahandler having the database connections</param>
        /// <param name="obj">an object of type Folders that have the ID to delete</param>
        /// <returns>true or false (succeed or error)</returns>
        public bool deleteAll(ClassDataHandler con, Folders obj)
        {
            


            FoldersDB objFoldersDB = new FoldersDB();


            List<Folders> lstFolders = getAllRelatedFolders(obj);
            List<Courses> lstCourses = getAllRelatedCourses(lstFolders);

            foreach (Folders objFolder in lstFolders)
            {
                objFolder.StatusID = 2;
                objFoldersDB.Update(con, objFolder);
            }

            CoursesDB objCoursesDB = new CoursesDB();

            foreach (Courses objCourse in lstCourses)
            {
                objCourse.StatusID = 2;
                objCoursesDB.Update(con, objCourse);
            }

            


            obj.StatusID = 2;
            objFoldersDB.Update(con, obj);


            return true;


        }

        /// <summary>
        /// this function will get all folder childs courses from its childs folders list
        /// </summary>
        /// <param name="lstFolders">the folders list</param>
        /// <returns>a list of all courses</returns>
        private List<Courses> getAllRelatedCourses(List<Folders> lstFolders)
        {
            List<Courses> lstRet = new List<Courses>();

            foreach (Folders objFolder in lstFolders)
            {
                lstRet.AddRange(objFolder.lstCourses);
            }


            return lstRet;

        }


        /// <summary>
        /// this recursive function to get all folders to update status to deleted on them
        /// </summary>
        /// <param name="obj">the object to get childeren</param>
        /// <returns>a list of its child folders</returns>
        private List<Folders> getAllRelatedFolders(Folders obj)
        {
            List<Folders> lstRet = new List<Folders>();

            lstRet.AddRange(obj.lstChild);

            foreach (Folders objChild in obj.lstChild)
            {
                lstRet.AddRange(getAllRelatedFolders(objChild));
            }

            return lstRet;
        }



        /// <summary>
        /// this function will delete the Folders and its subfolders
        /// </summary>
        /// <param name="obj">an object of type Folders that have the ID to delete</param>
        /// <returns>true or false (succeed or error)</returns>
        public bool deleteAll(Folders obj)
        {
            ClassDataHandler con = new ClassDataHandler(ConnectionStrings.LMS);
            con.OpenTransConnection(IsolationLevel.ReadUncommitted);
            bool bRet = false;
            try
            {

                bRet = deleteAll(con, obj);


                con.CommitTransConnection();

            }
            catch
            {
                con.RollbackTransConnection();
                return false;
            }


            return bRet;


        }




        ///// <summary>
        ///// this function will search for a folder by OR and like for strings
        ///// </summary>
        ///// <param name="con">Database handler having connection</param>
        ///// <param name="obj">the folder object to search for his values</param>
        ///// <returns>a list containing results</returns>
        //public List<Folders> SearchOrLike(ClassDataHandler con, Folders obj)
        //{
        //    string strWhere = "";
        //    List<DbParameter> lstParameters = new List<DbParameter>();


        //    if (obj.ID != -1)
        //    {
        //        DbParameter prmID = con.createParameter();
        //        prmID.ParameterName = "@ID";
        //        prmID.DbType = DbType.Int64;
        //        if (obj.ID == null)
        //        {
        //            prmID.Value = DBNull.Value;
        //        }
        //        else
        //        {
        //            prmID.Value = obj.ID;
        //        }
        //        strWhere += " [ID]=@ID OR ";
        //        lstParameters.Add(prmID);
        //    }





        //    if (obj.FolderNameAR != "")
        //    {
        //        DbParameter prmFolderNameAR = con.createParameter();
        //        prmFolderNameAR.ParameterName = "@FolderNameAR";
        //        prmFolderNameAR.DbType = DbType.String;
        //        if (obj.FolderNameAR == null)
        //        {
        //            prmFolderNameAR.Value = DBNull.Value;
        //        }
        //        else
        //        {
        //            prmFolderNameAR.Value = obj.FolderNameAR;
        //        }
        //        strWhere += " [FolderNameAR] LIKE @FolderNameAR OR ";
        //        lstParameters.Add(prmFolderNameAR);
        //    }





        //    if (obj.FolderNameEN != "")
        //    {
        //        DbParameter prmFolderNameEN = con.createParameter();
        //        prmFolderNameEN.ParameterName = "@FolderNameEN";
        //        prmFolderNameEN.DbType = DbType.String;
        //        if (obj.FolderNameEN == null)
        //        {
        //            prmFolderNameEN.Value = DBNull.Value;
        //        }
        //        else
        //        {
        //            prmFolderNameEN.Value = obj.FolderNameEN;
        //        }
        //        strWhere += " [FolderNameEN] LIKE @FolderNameEN OR ";
        //        lstParameters.Add(prmFolderNameEN);
        //    }





        //    if (obj.ParentID != null)
        //    {
        //        DbParameter prmParentID = con.createParameter();
        //        prmParentID.ParameterName = "@ParentID";
        //        prmParentID.DbType = DbType.Int64;
        //        if (obj.ParentID == null)
        //        {
        //            prmParentID.Value = DBNull.Value;
        //        }
        //        else
        //        {
        //            prmParentID.Value = obj.ParentID;
        //        }
        //        strWhere += " [ParentID]=@ParentID OR ";
        //        lstParameters.Add(prmParentID);
        //    }





        //    if (obj.StatusID != -1)
        //    {
        //        DbParameter prmStatusID = con.createParameter();
        //        prmStatusID.ParameterName = "@StatusID";
        //        prmStatusID.DbType = DbType.Int64;
        //        if (obj.StatusID == null)
        //        {
        //            prmStatusID.Value = DBNull.Value;
        //        }
        //        else
        //        {
        //            prmStatusID.Value = obj.StatusID;
        //        }
        //        strWhere += " [StatusID]=@StatusID OR ";
        //        lstParameters.Add(prmStatusID);
        //    }






        //    if (strWhere != "")
        //        strWhere = " where " + strWhere.Substring(0, strWhere.LastIndexOf("OR"));





        //    List<Folders> lstRet = Select(con, "", strWhere, lstParameters.ToArray());

        //    return lstRet;
        //}


        // /// <summary>
        ///// this function will search for a folder by OR and like for strings
        ///// </summary>
        ///// <param name="obj">the folder object to search for his values</param>
        ///// <returns>a list containing results</returns>
        //public List<Folders> SearchOrLike( Folders obj)
        //{
        //    ClassDataHandler con = new ClassDataHandler(_Connection, _Provider);

        //    con.OpenConnection();

        //    List<Folders> lstRet = SearchOrLike(con, obj);

        //    con.CloseConnection();

        //    return lstRet;

        //}


    }

}

                        